package bookstore.service;

import bookstore.handle.Msg;
import bookstore.mapper.UserMapper;
import bookstore.model.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    //注册
    public Object addUser(User user){
        Integer num=userMapper.selectCount(new QueryWrapper<User>().eq("name",user.getName()));
        if(num>0){
            return new Msg(0,"用户名已存在");
        }else{
            Integer id=userMapper.insert(user);
            return new Msg(200,"注册成功，去登录吧！");
        }
    }
    //登陆
    public Object login(Map mapPar){
       List<User> list= userMapper.selectByMap(mapPar);
       if(list.size()==1){
           Map map=new HashMap();
           map.put("name",list.get(0).getName());
           map.put("id",list.get(0).getId());
           return new Msg(200,"登陆成功",map);
       }else{
           return new Msg(0,"用户名或密码错误");
       }
    }
}
